var flagDataNot = true;// 查无结果
//加载grid
function jqGridSource(){
	jQuery("#grid-table").jqGrid({
		// direction: "rtl",//反方向显示
		postData:getParams(),// 参数
		url :path + "/stock/stockDecmil.do",
		datatype : 'json',
		mtype : 'POST',
		jsonReader : {  
        	root:"rows",//array或者List数据 
        	page: "page",//当前页码
        	total: "total",//总页数
        	records: "records",//总记录数
        	repeatitems: false
        }, 
		caption: "库存情况分析",
		autowidth: true,
		height: 'auto',// 高度
		colNames : [ '药品通用名', '生产企业名称','地区', '剂型', '制剂规格', '包装规格',  '批准文号','生产批次', '生产企业库存' 
		             ,'批发企业数','批发企业库存','原始ID','企业ID','省代码','零售企业库存','旧的生产ID','省code'],
		colModel : [
		             				
		             				{name : 'PHYSIC_NAME', index : 'PHYSIC_NAME',sortable : true,
		             					cellattr : function(rowId, tv,rawObject, cm, rdata) {
		             						//合并单元格
		             						return 'id=\'PHYSIC_NAME' + rowId + "\'"; 
		             					}
		             				},
		             				{name : 'ENT_NAME', index : 'ENT_NAME' ,sortable : true,
		             					cellattr : function(rowId, tv,rawObject, cm, rdata) {
		             						//合并单元格
		             						return 'id=\'ENT_NAME' + rowId + "\'"; 
		             					}
		             				},
		             				{name : 'DICT_NAME', index : 'DICT_NAME',sortable : true,
		             					cellattr : function(rowId, tv,rawObject, cm, rdata) {
		             						//合并单元格
		             						return 'id=\'DICT_NAME' + rowId + "\'"; 
		             					}
		             				},
		             				{name : 'PREPN_TYPE_NAME', index :'PREPN_TYPE_NAME' ,sortable : true,
		             					cellattr : function(rowId, tv,rawObject, cm, rdata) {
		             						//合并单元格
		             						return 'id=\'PREPN_TYPE_NAME' + rowId + "\'"; 
		             					}
		             				},
		             				{name : 'PREPN_SPEC', index : 'PREPN_SPEC',sortable : true,
		             					cellattr : function(rowId, tv,rawObject, cm, rdata) {
		             						//合并单元格
		             						return 'id=\'PREPN_SPEC' + rowId + "\'"; 
		             					}
		             				},
		             				{name : 'PKG_SPEC', index : 'PKG_SPEC',sortable : true ,
		             					cellattr : function(rowId, tv,rawObject, cm, rdata) {
		             						//合并单元格
		             						return 'id=\'PKG_SPEC' + rowId + "\'"; 
		             					}
		             				},
		             				{name : 'APPROVAL_NO_ID', index :'APPROVAL_NO_ID',sortable : false	}, 
		             				{name : 'PRODUCE_BATCH_NO', index :'PRODUCE_BATCH_NO',sortable : true },
		             				{name : 'proStock', index : 'proStock',sortable : true },
		             				{name : 'pfEntNum', index : 'pfEntNum',sortable : true },
		             				{name : 'pfStock', index : 'pfStock',sortable : true },//pfEntNum  lsStock  
		             				{name:'DRUG_ENT_BASE_INFO_ID',index:'DRUG_ENT_BASE_INFO_ID',hidden:true},
		             				{name:'ENT_INFO_ID',index:'ENT_INFO_ID',hidden:true},
		             				{name:'DICT_REGION_CODE',index:'DICT_REGION_CODE',hidden:true},
		             				{name : 'lsStock', index : 'lsStock',sortable : true },
		             				{name : 'OLD_PRODUCE_ENT_INFO_ID', index : 'OLD_PRODUCE_ENT_INFO_ID',sortable : true,hidden:true },
		             				{name : 'DICT_ID', index : 'DICT_ID',sortable : true,hidden:true }
		             		 ],
		             	viewrecords : true,
						rowNum:10,
						rowList:[5,10,20,50,100],
						pager : "#grid-pager",
						altRows: true,
						multiselect : false,// 多选
						multiboxonly : true,
		gridComplete : function() {  
			Merger("grid-table","DICT_NAME");
			Merger("grid-table","PHYSIC_NAME");
			Merger("grid-table","ENT_TYPE_NAME");
			Merger("grid-table","PREPN_SPEC");
			Merger("grid-table","PREPN_TYPE_NAME");
			Merger("grid-table","PREPN_SPEC");
			Merger("grid-table","PKG_SPEC"); 
			var rowNum = parseFloat($(this).getGridParam("records")).toFixed(3);
			if (rowNum > 0) {
				flagDataNot = true;
				// 如果有数据就隐藏
				$("#noData").parent().remove();
				$(".ui-jqgrid-sdiv").show();
				$("#grid-table").closest(".ui-jqgrid-bdiv").css({ 'overflow-y' : 'hidden' });
			} else {
				$(".ui-jqgrid-sdiv").hide();
				if(flagDataNot) {
					flagDataNot = false;
					//$("#grid-table").parent().append('<p align="center" id="noData"><font size="4">查无结果</font></p>');
				}
			} 
			 var ids = $("#grid-table").jqGrid("getDataIDs");
			   for(var i=0;i < ids.length;i++){ 
					var cl = ids[i]; 
					var rowdata = $("#grid-table").jqGrid("getRowData",cl);
					var provCodes = rowdata.DICT_REGION_CODE ;
					var newCode = provCodes.substring(0,2)+'0000';  
					//alert(rowdata.proStock);
					click1 = "<a style='color:blue;' href='#' onclick='window.open(\"http://gda.drugadmin.com/government/drugTrace/BIdrugTraceIndex.jsp?prodSeqNo="
						+rowdata.DRUG_ENT_BASE_INFO_ID+"&entSeqNo="+rowdata.OLD_PRODUCE_ENT_INFO_ID+"&batchNo="+rowdata.PRODUCE_BATCH_NO+"&entInfoSeqNo="+rowdata.OLD_PRODUCE_ENT_INFO_ID+"&provCode="+newCode+"\")' >"
						+rowdata.PRODUCE_BATCH_NO+"</a>"; 
					$("#grid-table").jqGrid("setRowData",cl,{PRODUCE_BATCH_NO:click1});  
					click2 = "<a style='color:blue;' href='#' onclick='preciseEntStock(\""+rowdata.DICT_NAME+"\",\""+rowdata.DICT_ID+"\",\""+rowdata.PRODUCE_BATCH_NO+"\","+1+")' >"
						+rowdata.proStock+"</a>";    
				 
					click3 = "<a style='color:blue;' href='#' onclick='preciseEntStock(\""+rowdata.DICT_NAME+"\",\""+rowdata.DICT_ID+"\",\""+rowdata.PRODUCE_BATCH_NO+"\","+2+")' >"
						+rowdata.pfStock+"</a>";   
					click4 = "<a style='color:blue;' href='#' onclick='preciseEntStock(\""+rowdata.DICT_NAME+"\",\""+rowdata.DICT_ID+"\",\""+rowdata.PRODUCE_BATCH_NO+"\","+4+")' >"
						+rowdata.lsStock+"</a>";  
					//,proStock:click2,pfStock:click3,lsStock:click4  
					$("#grid-table").jqGrid("setRowData",cl,{PRODUCE_BATCH_NO:click1 }); 
					if(rowdata.proStock != 0 )
						{
							$("#grid-table").jqGrid("setRowData",cl,{proStock:click2}); 
						}//,proStock:click2,pfStock:click3,lsStock:click4
					if(rowdata.pfStock != 0)
						{
							$("#grid-table").jqGrid("setRowData",cl,{pfStock:click3 });
						}
					if(rowdata.lsStock != 0){
						$("#grid-table").jqGrid("setRowData",cl,{lsStock:click4 });
					}
				}
		},
		loadComplete : replaceCss
	});
}; 
//链接点击事件
function getPath(url, DICT_NAME, DICT_ID, productNO,flag) { 
	var tempPath = ""; 
	tempPath = path 
		+ "/forward.do?url=/stock/"+url+"&params={"
		+ "dict_name:"+DICT_NAME+",dict_id:"+DICT_ID+",productNO:"+productNO
		+ ",flag:"+flag+"}";
	return tempPath;
}
//生产企业库存钻取
function preciseEntStock(DICT_NAME, DICT_ID, productNO,flag) {
path = getPath("decmilStockDetail",DICT_NAME, DICT_ID, productNO,flag);		
window.location.href = path ;
};

//当省用户登入时调用 
function getProNameByCode(userRoleDict){
	var getProNameUrl = path+"/condition/getProNameByCode.do";
	$.ajax({
		url : getProNameUrl,
		data : {
			"userRoleDict" : userRoleDict
		},
		type : 'post',
		//dataType : "json",
		async : false,
		success : function(data) {
			tempProName = data.split(',')[0];
			tempProValue = data.split(',')[1];
			$("#provName").val(data.split(',')[0]);
			$("#provNameValue").val(data.split(',')[1]);
		}
	});
}

 
$(function() { 
	// 加载页面函数
	loadPagesFun(); 
//制剂规格联动
var urlPrepnSpec = path+"/condition/queryLinkagePrepnSpecs.do";

$("#prenpSpec").autoText({
		url : urlPrepnSpec,
	param:["prepnSpec","prenpSpec","physicNames","physicName"]
	});


	//企业名称
	var urlEnt=path+"/condition/getEENames.do";
	$("#entName").autoText({
		url : urlEnt,
		param:["entName","entName"]
	});
	//生产批次
	var urlBatchNo=path+"/condition/getProdProduceBatchNo.do";
	$("#batchNo").autoText({
		url : urlBatchNo,
		param:["prodProduceBatchNo","batchNo"]
	});
	 //获取批准文号
	   var urlAppno = path+"/stockTip/approvNoQuery.do";
	   $("#appNO").autoText({
			url : urlAppno,
			param:["approvalNo","appNO"]
		});
});
//取得参数
function getParams(){
	var v1 = $("#physicNameValue").val();
	var dict_ids='';
	if(user_role_type == user_role_type_gov){
		dict_ids = $("#provNameValue").val();			
	}else if(user_role_type == user_role_type_prov){
		dict_ids = $("#cityNameValue").val();
	}else if(user_role_type == user_role_type_city
			||user_role_type == user_role_type_area){
		dict_ids = $("#areaNameValue").val();
	}
	var v3 = $("#batchNoValue").val(); 
	var v4 = $("#prenpSpecValue").val(); 
	var v5 = $("#entNameValue").val(); 
	var v6 = $("#appNOValue").val(); 	 
	var p={};
	p.physicId=v1; 
	p.dict_ids=dict_ids;  
	p.prenpSpec = v4;
	p.prodProduceBatchNo= v3;
	p.entId = v5;
	p.approvalNo=v6;
	return p;
}